//
//  Collecton.m
//  Echo
//
//  Created by Liao on 14/12/24.
//  Copyright (c) 2014年 sina. All rights reserved.
//

#import "Collecton.h"
#import "EchoApiClient.h"
#import "CurrentUser.h"
#import "Md5Support.h"

@implementation Collecton

@synthesize collection_id = _collection_id;
@synthesize tweet = _tweet;
@synthesize owner_id = _owner_id;

+ (NSURLSessionDataTask *)collectionsOfCurrentUserBaseOnCursor:(NSString *)collection_id WithBlock:(void (^)(NSArray *collections, NSError *error))block {
    if (![CurrentUser SharedCurrentUser].isLogin) {
        if (block) {
            NSError *error = [NSError errorWithDomain:@"need login" code:ERROR_CODE_NEED_LOGIN userInfo:nil];
            block([NSArray array], error);
        }
        return nil;
    }
    NSDictionary *parameters = @{@"uid": [CurrentUser SharedCurrentUser].uid, @"token":[CurrentUser SharedCurrentUser].token, @"cursor":collection_id};
    return [[EchoApiClient sharedClient] GET:ECHO_API_COLLECTION_LIST parameters:parameters success:^(NSURLSessionDataTask * __unused task, id JSON) {
        if ([JSON valueForKey:@"error_code"]) {
            if (block) {
                NSError *error = [NSError errorWithDomain:[JSON valueForKey:@"error_msg"] code:ERROR_CODE_ECHO_API_FAILED userInfo:nil];
                block([NSArray array], error);
            }
        } else {
            NSMutableArray *mutableCollections = [NSMutableArray arrayWithCapacity:[JSON count]];
            for (NSDictionary *attributes in JSON) {
                Collecton *collection = [[Collecton alloc] init];
                collection.owner_id = [CurrentUser SharedCurrentUser].uid;
                collection.collection_id = [attributes valueForKey:@"collection_id"];
                collection.tweet = [[Tweet alloc] initWithAttributes:[attributes valueForKey:@"content"]];
                collection.tweet.is_collected = YES;
                [mutableCollections addObject:collection];
            }
        
            if (block) {
                block([NSArray arrayWithArray:mutableCollections], nil);
            }
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSArray array], error);
        }
    }];
}
+ (NSURLSessionDataTask *)addTweet:(Tweet *)tweet ToCollectionsByCurrentUserWithBlock:(void (^)(Collecton *new_collection, NSError *error))block {
    if (![CurrentUser SharedCurrentUser].isLogin) {
        if (block) {
            NSError *error = [NSError errorWithDomain:@"need login" code:ERROR_CODE_NEED_LOGIN userInfo:nil];
            block(nil, error);
        }
        return nil;
    }
    NSString *check = [[[CurrentUser SharedCurrentUser].uid stringByAppendingString:tweet.mid] md5];
    NSDictionary *parameters = @{@"uid": [CurrentUser SharedCurrentUser].uid, @"tid":tweet.mid, @"check":check};
    return [[EchoApiClient sharedClient] POST:ECHO_API_COLLECTION_ADD parameters:parameters success:^(NSURLSessionDataTask * __unused task, id JSON) {
        if ([JSON valueForKey:@"error_code"]) {
            if (block) {
                NSError *error = [NSError errorWithDomain:[JSON valueForKey:@"error_msg"] code:ERROR_CODE_ECHO_API_FAILED userInfo:nil];
                block(nil, error);
            }
        } else {
            Collecton *collection = [[Collecton alloc] init];
            collection.owner_id = [CurrentUser SharedCurrentUser].uid;
            collection.collection_id = [JSON valueForKey:@"collection_id"];
            collection.tweet = tweet;
            
            if (block) {
                block(collection, nil);
            }
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block(nil, error);
        }
    }];

}
+ (NSURLSessionDataTask *)delTweet:(Tweet *)tweet FromCollectionsByCurrentUserWithBlock:(void (^)(NSError *error))block {
    if (![CurrentUser SharedCurrentUser].isLogin) {
        if (block) {
            NSError *error = [NSError errorWithDomain:@"need login" code:ERROR_CODE_NEED_LOGIN userInfo:nil];
            block(error);
        }
        return nil;
    }
    NSString *check = [[[CurrentUser SharedCurrentUser].uid stringByAppendingString:tweet.mid] md5];
    NSDictionary *parameters = @{@"uid": [CurrentUser SharedCurrentUser].uid, @"tid":tweet.mid, @"check":check};
    return [[EchoApiClient sharedClient] POST:ECHO_API_COLLECTION_DEL parameters:parameters success:^(NSURLSessionDataTask * __unused task, id JSON) {
        if ([JSON valueForKey:@"error_code"]) {
            if (block) {
                NSError *error = [NSError errorWithDomain:[JSON valueForKey:@"error_msg"] code:ERROR_CODE_ECHO_API_FAILED userInfo:nil];
                block(error);
            }
        } else {
            if (block) {
                block(nil);
            }
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block(error);
        }
    }];
}

+ (NSURLSessionDataTask *)checkTweets:(NSArray *)tweets AreCollectedByCurrentUserWithBlock:(void (^)(NSArray *checked_tweets, NSError *error))block {
    if (![CurrentUser SharedCurrentUser].isLogin) {
        if (block) {
            NSError *error = [NSError errorWithDomain:@"need login" code:ERROR_CODE_NEED_LOGIN userInfo:nil];
            block(tweets, error);
        }
        return nil;
    }
    NSString *tids = @"";
    for (Tweet *each_tweet in tweets) {
        tids = [tids stringByAppendingString:each_tweet.mid];
    }
    NSDictionary *parameters = @{@"uid": [CurrentUser SharedCurrentUser].uid, @"tids":tids};
    return [[EchoApiClient sharedClient] GET:ECHO_API_COLLECTION_CHECK parameters:parameters success:^(NSURLSessionDataTask * __unused task, id JSON) {
        if ([JSON valueForKey:@"error_code"]) {
            if (block) {
                NSError *error = [NSError errorWithDomain:[JSON valueForKey:@"error_msg"] code:ERROR_CODE_ECHO_API_FAILED userInfo:nil];
                block(tweets, error);
            }
        } else {
            NSDictionary *check_rcd = @{};
            for (NSDictionary *each in JSON) {
                [check_rcd setValue:[each valueForKey:@"is_collected"] forKey:[each valueForKey:@"tid"]];
            }
            for (Tweet *tweet in tweets) {
                if ([[check_rcd valueForKey:tweet.mid] isEqualToString:@"1"]) {
                    tweet.is_collected = YES;
                }
            }
            if (block) {
                block(tweets, nil);
            }
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block(tweets, error);
        }
    }];
}

@end
